package com.lyw.utils;

import com.lyw.exception.GlobalException;

import java.util.regex.Pattern;

public class SimpleFormatterUtil {

    public static final Pattern INTEGER = Pattern.compile("^[0-9]+$");
    public static final Pattern DOUBLE = Pattern.compile("(([0-9]+).([0-9]+))");
    public static final Pattern BOOLEAN = Pattern.compile("(true|false)");
    public static final Pattern STRING = Pattern.compile("([a-z]|[A-z])+");
    public static final Pattern STRING_NOT_EMPTY = Pattern.compile("\\S+");
    public static final Pattern CHAR = Pattern.compile("([a-z]|[A-z])");


    private SimpleFormatterUtil() {}

    public static Class<?> formatterTypeByValue(String value) {

        if (value == null || "".equals(value)) {
            throw new GlobalException("获取当前字符串类型是出现问题！");
        }

        if (INTEGER.matcher(value).find()) {
            return Long.class;
        } else if (BOOLEAN.matcher(value).find()) {
            return Boolean.class;
        } else if (DOUBLE.matcher(value).find() || STRING.matcher(value).find() || STRING_NOT_EMPTY.matcher(value).find()) {
            return String.class;
        } else if (CHAR.matcher(value).find()) {
            return Character.class;
        } else {
            throw new GlobalException("当前 [ " + value + " ] 类型不支持，请联系管理员！");
        }
    }
}
